package ro.msg.quiz.repository;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * Author: Vlad This class extends Question class. Single choice question is a
 * particular type of question. It has one correct answer.
 */
@XmlRootElement(name = "SingleChoiceQ")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class SingleChoiceQ extends Question {
	// options is an array which contains the options of the Question
	private ArrayList<String> options = new ArrayList<String>();
	// an integer for the correctOption of the question
	private int correctOption;
	private static final int type = ClassType.SINGLE_CHOICE_QUESTION.getCode();
	private static String dateFormatString = "dd.MM.yyyy";

	/**
	 * Constructor
	 */
	public SingleChoiceQ() {
		this.setId(-1);
		this.setType(1);
		this.setText(null);
		this.setCorrectOption(0);
		this.setOptions(null);

		DateFormat dateFormat = new SimpleDateFormat(dateFormatString);
		Date date = new Date();
		setLastEdited(dateFormat.format(date));
	}

	/**
	 * Constructor
	 * 
	 * @param id
	 *            The id of question.
	 * @param text
	 *            The text of question.
	 * @param options
	 *            The options of question.
	 * @param correctOption
	 *            The correct option of question.
	 */
	public SingleChoiceQ(int id, String text, ArrayList<String> options,
			int correctOption) {
		this.setId(id);
		this.setType(1);
		this.setText(text);
		this.setOptions(options);
		this.setCorrectOption(correctOption);

		DateFormat dateFormat = new SimpleDateFormat(dateFormatString);
		Date date = new Date();
		setLastEdited(dateFormat.format(date));
	}

	/**
	 * 
	 * @return Returns the correct option of the question.
	 */
	@XmlElement
	public int getCorrectOption() {
		return correctOption;
	}

	/**
	 * @return Returns the correct answer
	 */
	@Override
	public ArrayList<String> getAnswers() {
		ArrayList<String> answer = new ArrayList<String>();
		answer.add(Integer.toString(getCorrectOption()));
		return answer;
	}

	/**
	 * Sets the correct option of question.
	 * 
	 * @param correctOption
	 *            The correct option to be set.
	 */
	public void setCorrectOption(int correctOption) {
		this.correctOption = correctOption;

		DateFormat dateFormat = new SimpleDateFormat(dateFormatString);
		Date date = new Date();
		setLastEdited(dateFormat.format(date));
	}

	/**
	 * 
	 * @return Returns a list with options of question.
	 */
	@XmlElementWrapper(name = "options")
	@XmlElement(name = "option")
	@Override
	public ArrayList<String> getOptions() {
		return options;
	}

	/**
	 * Sets the list of options to question.
	 * 
	 * @param options
	 *            The options list of question to be set.
	 */
	public void setOptions(ArrayList<String> options) {
		this.options = options;

		DateFormat dateFormat = new SimpleDateFormat(dateFormatString);
		Date date = new Date();
		setLastEdited(dateFormat.format(date));
	}

	/**
	 * @return Returns the type of question.
	 */
	@Override
	public int getType() {
		return type;
	}

	/**
	 * This method prints the id, text, list of option and correct option of
	 * question.
	 */
	@Override
	public String toString() {
		return getId() + " " + getText() + " " + options.toString() + " "
				+ correctOption + " " + getLastEdited();
	}
}
